<?php

declare(strict_types=1);

namespace App\Amqp\Consumers;

use App\FundConf;
use App\Service\HscoinBillService;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Amqp\Result;
use PhpAmqpLib\Message\AMQPMessage;
use App\Service\FundBillService;
use Hyperf\Di\Annotation\Inject;

#[Consumer(exchange: "goldbean", routingKey: "goldbean", queue: "goldbean", nums: 1, enable: true)]
class HscoinConsumer extends ConsumerMessage
{
    #[Inject]
    private HscoinBillService $billService;


    public function consumeMessage($queueData, AMQPMessage $message): Result
    {
        $opt = $queueData['opt'] ?? [];
        $data = $queueData['data'] ?? [];
        $category = $opt['goldbean']['category'] ?? 0;
        if (!isset(FundConf::$categoryExpenseLabelMap[$category]) && !isset(FundConf::$categoryIncomeLabelMap[$category])) {
            echo '消息错误';
            return Result::DROP;
        }
        if (!isset($data['id']) or !isset($data['amount']) or !isset($data['uid']) or !isset($data['mark'])) {
            echo '消息错误';
            return Result::DROP;
        }
        $link_amount = $data['link_amount'] ?? 0;
        $platform_id = $data['platform_id'] ?? 0;
        $rs = $this->billService->add($category, $data['id'], $link_amount, $data['amount'], $data['uid'], $data['mark'], $platform_id);

        return Result::ACK;


    }

    public function isEnable(): bool
    {
        return parent::isEnable();
    }


}
